/**
 * 图像融合(地表)
 * @param       viewer              Mars3d实例对象
 * 
 * @function    addSurfacePatch     添加图像
 * @function    updateSurfacePatch  更新图像
 * @function    deleteSurfacePatch  删除图像
 * @returns 
 */
export function BQGIS_initFacePatch(viewer){
    return new BQGIS_FacePatch(viewer)
}

export function BQGIS_FacePatch(viewer){
    this.viewer = viewer
    this.graphicLayer = null
    this.surfaceDatas=[]
    this.surfaceVideoData = null
    this.surfaceVideos=[]
}
function CreateVideoDom(){
    let videoElement = mars3d.DomUtil.create("video", "", document.body)
    videoElement.setAttribute("muted", "muted")
    videoElement.setAttribute("autoplay", "autoplay")
    videoElement.setAttribute("loop", "loop")
    videoElement.setAttribute("crossorigin", "")
    videoElement.setAttribute("controls", "")
    videoElement.style.display = "none"
    return videoElement
}
BQGIS_FacePatch.prototype.setSurfacePatch = function(VideoImageArea){
    let suffix = VideoImageArea.url.substring(VideoImageArea.url.lastIndexOf(".")+1)
    if(suffix==="flv"){
        if( window.flvjs.isSupported()){
            let videoElement = CreateVideoDom()   
            let flvPlayer = window.flvjs.createPlayer({
                type: "flv",
                url: VideoImageArea.url
            })
              flvPlayer.attachMediaElement(videoElement)
              flvPlayer.load()
              this.surfaceVideoData = flvPlayer
              return videoElement
        }
        else{
            return undefined
        }        
    }
    else if(suffix==="mp4"){
        let videoElement = CreateVideoDom()
        const sourceContainer = mars3d.DomUtil.create("source", "", videoElement)
        sourceContainer.setAttribute("src", VideoImageArea.url)
        sourceContainer.setAttribute("type", "video/mp4")
        this.surfaceVideoData = videoElement
        return videoElement

    }
    else{
        return VideoImageArea.url
    }
    

}
BQGIS_FacePatch.prototype.addSurfacePatch=function(VideoImageArea,callBack=()=>{}){
    if(!this.graphicLayer){
        this.graphicLayer = new mars3d.layer.GraphicLayer()           
        this.viewer.addLayer(this.graphicLayer)
        this.graphicLayer.on(mars3d.EventType.click,function(event){
            
            console.log("监听layer，单击了对象", event.graphic)
            return callBack(event.graphic)
        })
    } 
    var surface = this.setSurfacePatch(VideoImageArea)
    if(surface){
        let graphic = new mars3d.graphic.PolygonEntity({
            positions:VideoImageArea.points,
            style:{
                color:VideoImageArea.color,
                material:surface,
                clampToGround:true
            },
            id:VideoImageArea.keyId
        })
        this.graphicLayer.addGraphic(graphic)
        this.surfaceDatas.push(graphic)
        this.surfaceVideos.push({keyid:graphic.id,surfaceVideoData:this.surfaceVideoData,surface:surface})
        return graphic
    }
}
BQGIS_FacePatch.prototype.updateSurfacePatch=function(VideoImageArea){
    let data
    if("keyType" in VideoImageArea){
        data = this.surfaceDatas.filter((item)=>{
            return item[VideoImageArea[keyType]]===VideoImageArea.keyId
        })
    }
    else{
        data = this.surfaceDatas.filter((item)=>{
            return item.id===VideoImageArea.keyId
        })
    }
  
    let suffix = VideoImageArea.url.substring(VideoImageArea.url.lastIndexOf(".")+1)
    if(data.length>0){
        data.filter((vaule)=>{
            //let datat= this.surfaceDatas.find((item)=>{
                //item.id===vaule.id
            //})
            if(suffix==="flv"){
                let fdata= this.surfaceVideos.find((item)=>{
                    return item.keyid===vaule.id
                })
                if(fdata){
                    fdata.surfaceVideoData.url=VideoImageArea.url
                }
                else{
                    return
                }
            }
            else if(suffix==="mp4"){
                let fdata= this.surfaceVideos.find((item)=>{
                    return item.keyid===vaule.id
                })
                if(fdata){
                    fdata.surface.children[0].src=VideoImageArea.url
                    vaule.style={
                        material:fdata.surface,
                        color:VideoImageArea.color
                    }
                }
                else{
                    var surface = this.setSurfacePatch(VideoImageArea)
                    let datadt= this.surfaceVideos.find((item)=>{
                        return item.keyid===vaule.id
                    })
                    if(datadt){
                        let findex= this.surfaceVideos.indexOf(datadt)
                        if(findex>-1){
                            this.surfaceVideos.splice(findex,1,{keyid:vaule.id,surfaceVideoData:null,surface:surface});                   
                        } 
                        else{
                            this.surfaceVideos.push({keyid:vaule.id,surfaceVideoData:null,surface:surface}) 
                        }                       
                    }
                    else{
                        this.surfaceVideos.push({keyid:vaule.id,surfaceVideoData:null,surface:surface}) 
                    }
                    vaule.style={
                        material:surface,
                        color:VideoImageArea.color
                    }
                }
             
            }
            else{
                vaule.style={
                    material:VideoImageArea.url,
                    color:VideoImageArea.color
                }
                //vaule.material=VideoImageArea.url
            }
            //vaule.color=VideoImageArea.color
    
        })

    }
}
BQGIS_FacePatch.prototype.deleteSurfacePatch=function(ScreenObj){
    let data
    if("keyType" in ScreenObj){
        data = this.surfaceDatas.filter((item)=>{
            return item[ScreenObj["keyType"]]===ScreenObj.keyId
        })
    }
    else{
        data = this.surfaceDatas.filter((item)=>{
            return item.id===ScreenObj.keyId
        })
    }
 
    if(this.graphicLayer&&data.length>0){
        data.forEach((vaule)=>{
            this.graphicLayer.removeGraphic(vaule)
            let index= this.surfaceDatas.indexOf(vaule)
            if(index>-1){
                this.surfaceDatas.splice(index,1);
            }
            let fdata= this.surfaceVideos.find((item)=>{
                return item.keyid===vaule.id
            })
            if(fdata){
                let findex= this.surfaceVideos.indexOf(fdata)
                if(findex>-1){
                    this.surfaceVideos.splice(findex,1);
                }
            }  
        })
           
    }
}